package com.hospital.yang.fchua.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hospital.yang.fchua.entity.DoctorMedicine;
import com.hospital.yang.fchua.entity.FrontdeskMoney;
import com.hospital.yang.fchua.service.DoctorMedicineService;
import com.hospital.yang.fchua.service.FrontdeskMoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @description：
 * @author：yzh
 * @date：2021/9/22 19:57
 * @versions：v1.0
 */
@Controller
@RequestMapping("/Medicine")
public class DoctorMedicineController {

    @Autowired
    private DoctorMedicineService doctorMedicineService;

    @Autowired
    private FrontdeskMoneyService frontdeskMoneyService;

    /**
     * 药订单分页查询
     * @param name
     * @param curr
     * @param pageSize
     * @return
     */
    @RequestMapping("/MedicineList")
    public String MedicineList(Model model, @RequestParam(value = "name", defaultValue = "") String name, @RequestParam(value = "curr", defaultValue = "1") Integer curr, @RequestParam(defaultValue = "5") Integer pageSize) {
        List<DoctorMedicine> doctorMedicines = doctorMedicineService.MedicineList(name, curr, pageSize);
        PageInfo<DoctorMedicine> page = new PageInfo<>(doctorMedicines);
        model.addAttribute("page", page);
        model.addAttribute("name", name);
        return "frontdesk/fchua/guide";
    }

    @RequestMapping("/payment")
    @ResponseBody
    public String listdate(@RequestParam(value = "userid", defaultValue = "") Integer userid, @RequestParam(value = "date", defaultValue = "") String date) {
        List<DoctorMedicine> listdate = doctorMedicineService.listdate(userid, date);
        double money = doctorMedicineService.money(listdate);
        boolean flag = doctorMedicineService.pdMoney(userid, money);
        if (flag) {
            String drugs = doctorMedicineService.drugs(listdate);
            frontdeskMoneyService.insert(new FrontdeskMoney(null, userid, new Date(), drugs, money));
            doctorMedicineService.update(listdate);
        }
        return JSON.toJSONString(flag);
    }

    @RequestMapping("/money")
    @ResponseBody
    public String money(@RequestParam(value = "userid", defaultValue = "") Integer userid, @RequestParam(value = "date", defaultValue = "") String date) {
        List<DoctorMedicine> listdate = doctorMedicineService.listdate(userid, date);
        double money = doctorMedicineService.money(listdate);
        return JSON.toJSONString(money);
    }

    @RequestMapping("/details")
    @ResponseBody
    public String details(@RequestParam(value = "userid", defaultValue = "") Integer userid, @RequestParam(value = "date", defaultValue = "") String date) {
        List<DoctorMedicine> listdate = doctorMedicineService.listdate(userid, date);
        return JSON.toJSONString(listdate);
    }

}
